/*
 * Copyright 2009 Brett Morgan.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.domesticmouse.waveoteditor.client;

import com.domesticmouse.waveoteditor.client.canvas.Canvas;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

public class WaveEditor implements EntryPoint {

	private ClientState clientState;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		final Button lineButton = new Button("Line");
		RootPanel.get("lineButtonContainer").add(lineButton);
		lineButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				clientState.onLineButtonSelected();
			}
		});

		final Button rectangleButton = new Button("Rectangle");
		RootPanel.get("rectangleButtonContainer").add(rectangleButton);
		rectangleButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				clientState.onRectangleButtonSelected();
			}
		});

		final Button circleButton = new Button("Circle");
		RootPanel.get("circleButtonContainer").add(circleButton);
		circleButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				clientState.onCircleButtonSelected();
			}
		});

		final Button clearButton = new Button("Clear");
		RootPanel.get("clearButtonContainer").add(clearButton);
		clearButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				clientState.onClearButtonSelected();
			}
		});

		// TODO: Make a colour selector drop down.
		RootPanel.get("colourSelectorContainer");

		final Canvas canvas = Canvas.wrap(RootPanel.get("canvas").getElement());
		canvas.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				final int x = event.getNativeEvent().getClientX() - canvas.getAbsoluteLeft();
				final int y = event.getNativeEvent().getClientY() - canvas.getAbsoluteTop();
				clientState.onCanvasClick(x, y);
			}
		});

		clientState = new ClientState(canvas, new ClientState.HintDisplay() {

			@Override
			public void display(String hint) {
				RootPanel.get("hintDiv").getElement().setInnerHTML(hint);
			}
		}, new ClientState.ServerCommsDisplay() {

			@Override
			public void display(String serverComms) {
				RootPanel.get("serverCommsDiv").getElement().setInnerHTML(serverComms);
			}
		});

	}

}
